package com.sanxiau.dormitory.yang.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.sanxiau.dormitory.yang.entity.LoginRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        //添加shiro的内置过滤器
        /*
        * anon：无需认证就可以访问
        * authc：必须认证才能访问
        * user：必须拥有 记住我 功能才能使用
        * perms：拥有对某个资源的权限才能访问
        * role：拥有某个角色权限才能访问
        * */
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/assets/**","anon");
        filterMap.put("/charts/**","anon");
        filterMap.put("/css/**","anon");
        filterMap.put("/datatable/**","anon");
        filterMap.put("/font/**","anon");
        filterMap.put("/fonts/**","anon");
        filterMap.put("/images/**","anon");
        filterMap.put("/js/**","anon");
        filterMap.put("/lay/**","anon");
        filterMap.put("/login/userLogin1","anon");
        filterMap.put("/login/sendVerify","anon");
        filterMap.put("/login/userLogin2","anon");
        /*filterMap.put("/*.html","authc");*/
        filterMap.put("/**","authc");
        bean.setLoginUrl("/login/Login");
        bean.setUnauthorizedUrl("/login/Login");
        bean.setFilterChainDefinitionMap(filterMap);
        //设置登陆请求
        return bean;
    }

    @Bean("securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("loginRealm") LoginRealm loginRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联loginRealm
        securityManager.setRealm(loginRealm);
        return securityManager;
    }

    @Bean
    public LoginRealm loginRealm(){
        return new LoginRealm();
    }
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

}
